<!DOCTYPE html>
<html>

<head>
    <title>Online Store Costumer Page</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
</head>

<body>
    <header>
        <h2>Online Store Costumer page</h2>
    </header>
    <main>
        <div class="content">
            <h3>
                <?php
                session_start();
                $usr = $_SESSION['user'];
                echo "Welcome! Your customer id is $usr";
                ?>
                <button type="button" class="styledBtn" onclick="window.location.href='LoginPage.html'">Logout</button>
            </h3>
        </div>
        <div class="content">
            <h2 id="top">List of Products</h2>
            <hr>
            <div>
                <label style="font-weight: bold;color:#464646;">Search for product:</label>
                <br>
                <form action="customerSearch.php" method="POST" class="searchForm">
                    <select name="mode">
                        <option value="pid">By Product id</option>
                        <option value="pcat">By Category</option>
                        <option value="price">By Price</option>
                    </select>
                    <input type="text" name="search" required>
                    <button type="submit">Search</button>
                </form>
                <hr>
                <form action="customerList.php" method="POST" class="searchForm">
                <label>Order list by:</label>
                    <select name="method">
                        <option value="product_id">Product id</option>
                        <option value="product_category_name">Category</option>
                        <option value="price">Price</option>
                    </select>
                    <select name="order">
                        <option value="ASC">Asc</option>
                        <option value="DESC">Desc</option>
                    </select>
                    <button type="submit">Order List</button>
                </form>
                <a href="#bottom" style="float:right;">jump to bottom</a>
                <br><br>
            </div>
            <!--table to display items-->
            <?php
            include "connectStoreDB.php";

            $method=$_POST["method"];
            $order=$_POST["order"];

            if($method=="product_id"&&$order=="ASC"){
                header("Location: customer.php");
            }

            $sql = "SELECT * FROM products ORDER BY $method $order;";

            $result = mysqli_query($conn, $sql);

            //check if result is empty
            if (mysqli_num_rows($result) > 0) {

                //print the heading of table
                echo "<table border='1'>";
                echo "<tr>";

                //print each column name
                while ($property = mysqli_fetch_field($result)) {
                    echo "<th>" . $property->name . "</th>";
                }
                echo "</tr>";

                //get number of columns
                $fields = mysqli_num_fields($result);

                //Get each row data
                while ($row = mysqli_fetch_array($result)) {
                    echo "<tr>";
                    for ($f = 0; $f < $fields; $f++) {
                        echo "<td>" . $row[$f] . "</td>";
                    }
                }

                echo "</table>";
                echo mysqli_num_rows($result) . " results";
            } else {
                echo "0 results";
            }

            //close connection
            mysqli_close($conn);
            ?>
            <br>
            <a id="bottom" href="#top">back to top</a>
            <hr>
        </div>
    </main>
</body>

</html>